package com.ruoyi.web.service.custom.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.gitee.deeround.jdbchelper.DbAction;
import com.ruoyi.core.entity.BaseMap;
import com.ruoyi.core.entity.PageInfo;
import com.ruoyi.encrypt.util.SM3Util;
import com.ruoyi.web.entity.system.SysModInfoVo;
import com.ruoyi.web.exception.ApiException;
import com.ruoyi.web.service.custom.SysUserModDataService;
import com.ruoyi.web.service.impl.SysModDataServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class SysUserModDataServiceImpl extends SysModDataServiceImpl implements SysUserModDataService {

    @Value("${spring.profiles.active}")
    private String active;

    @Autowired
    private DbAction dbAction;

    @Override
    public PageInfo<BaseMap> page(String modId, BaseMap entity) {
        PageInfo<BaseMap> page = super.page(modId, entity);
        for (BaseMap baseMap : page.getList()) {
            skipPassword(baseMap);
        }
        return page;
    }

    @Override
    public List<BaseMap> list(String modId, BaseMap entity) {
        List<BaseMap> list = super.list(modId, entity);
        for (BaseMap baseMap : list) {
            skipPassword(baseMap);
        }
        return list;
    }

    @Override
    public BaseMap add(String modId, BaseMap entity) {
        encryptPassword(entity);
        return super.add(modId, entity);
    }

    @Override
    public BaseMap edit(String modId, BaseMap entity) {
        if ("demo".equals(active)) {
            throw new ApiException("演示模式，不允许修改用户");
        }
        encryptPassword(entity);
        return super.edit(modId, entity);
    }

    @Override
    public void delete(String modId, BaseMap entity) {
        if ("demo".equals(active)) {
            throw new ApiException("演示模式，不允许删除用户");
        }
        super.delete(modId, entity);
    }

    @Override
    public BaseMap getById(String modId, BaseMap entity) {
        BaseMap baseMap = super.getById(modId, entity);
        skipPassword(baseMap);
        return baseMap;
    }

    @Override
    protected void updateData(SysModInfoVo modInfo, BaseMap data, List<String> ignoreColumns, BaseMap fixedData) {

        if (data.isEmpty("password")) {
            if (ignoreColumns == null) {
                ignoreColumns = new ArrayList<>();
                ignoreColumns.add("password");
            }
        }

        super.updateData(modInfo, data, ignoreColumns, fixedData);
    }

    private void encryptPassword(BaseMap entity) {
        String password = entity.getString("password");
        if (!StringUtils.isEmpty(password)) {
            //对密码进行加密
            String encrypt = SM3Util.encrypt(password);
            entity.put("password", encrypt);
        }
    }

    private void skipPassword(BaseMap entity) {
        //对密码进行过滤
        entity.put("password", "");
    }

}
